package com.grq.mis.validate;

import com.grq.mis.entity.Role;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import org.springframework.util.StringUtils;

public class RoleValidate {

    public static void add(Role role) {
        // name
        if (StringUtils.isEmpty(role.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "角色名称不能为空");
        }
        if (role.getName().length() > 50) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "角色名称最多为50字");
        }

        // code
/*
        if (StringUtils.isEmpty(role.getCode())) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "角色编码不能为空");
        }
        if (role.getCode().length() > 50) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "角色编码最多为50字");
        }
*/

        // description
        if (!StringUtils.isEmpty(role.getDescription())) {
            if (role.getDescription().length() > 255) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "角色描述最多为255字符");
            }
        }
    }

    public static void update(Role role) {
        if (role.getId() == null) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "角色ID不能为空");
        }
        // name
        if (StringUtils.isEmpty(role.getName())) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "角色名称不能为空");
        }
        if (role.getName().length() > 50) {
            throw new BusinessException(ResultEnum.PARAM_ERROR, "角色名称最多为50字");
        }

        // code
/*        if (StringUtils.isEmpty(role.getCode())) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "角色编码不能为空");
        }
        if (role.getCode().length() > 50) {
            throw new GrqException(ResultEnum.PARAM_ERROR, "角色编码最多为50字");
        }*/

        // description
        if (!StringUtils.isEmpty(role.getDescription())) {
            if (role.getDescription().length() > 255) {
                throw new BusinessException(ResultEnum.PARAM_ERROR, "角色描述最多为255字符");
            }
        }
    }

}
